package com.example.qxfw.nyqx.controller;

import com.example.qxfw.nyqx.dto.AgriSrvFcstDto;
import com.example.qxfw.nyqx.service.AgriProductService;
import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.ny.AgriServiceIndex;
import com.example.qxfw.common.entity.ny.AgriSrvFcst;
import com.example.qxfw.nyqx.service.AgriServiceIndexService;
import com.example.qxfw.nyqx.service.AgriSrvFcstService;
import com.example.qxfw.nyqx.service.DailyAgriculturalForecastService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.text.ParseException;
import java.util.List;

/**
 * 日农事活动预报接口
 */
@Api(tags = "农气预报-日农事活动预报")
@RestController
@CrossOrigin
@RequestMapping("/DailyAgriculturalForecastController")
public class DailyAgriculturalForecastController {

    @Value("${filePath.active}")
    private String active;

    @Autowired
    private AgriProductService agriProductService;
    @Autowired
    private AgriServiceIndexService agriServiceIndexService;

    @Autowired
    private AgriSrvFcstService agriSrvFcstService;

    @Autowired
    private DailyAgriculturalForecastService dailyAgriculturalForecastService;

    @ApiOperation("农气预报-日农事活动预报-预报制作")
    @GetMapping("/ybzz")
    public SysResult ybzz(String indexName, String dateChar, String timeChar) throws ParseException {
//        System.out.println(dateChar);
//        System.out.println(indexName);
        boolean key = dailyAgriculturalForecastService.ybzz(indexName, dateChar, timeChar);
        if (key) {
            return SysResult.success();
        } else {
            return SysResult.fail("预报制作失败");
        }

    }

    @ApiIgnore
    @ApiOperation("农气预报-日农事活动预报-获取农事活动预报数据")
    @GetMapping("/getAgriServiceIndex")
    public SysResult getAgriServiceIndex(String indexName, String weatherLevel) {
        List<AgriServiceIndex> list = agriServiceIndexService.getList(indexName, weatherLevel);
        return SysResult.success(list);
    }

    @ApiIgnore
    @ApiOperation("农气预报-日农事活动预报-获取农事活动预报数据")
    @GetMapping("/getAgriSrvFcst")
    public SysResult getAgriSrvFcst(String forecastDate, String timeChar, String indexName) {
        List<AgriSrvFcst> list = agriSrvFcstService.getList(forecastDate, timeChar, indexName);
        return SysResult.success(list);
    }

    @ApiIgnore
    @ApiOperation("农气预报-日农事活动预报-删除农事活动预报数据")
    @GetMapping("/deleteAgriSrvFcst")
    public SysResult deleteAgriSrvFcst(String forecastDate, String timeChar, String indexName) {
        Integer rows = agriSrvFcstService.delete(forecastDate, timeChar, indexName);
        return SysResult.success(rows);
    }

    @ApiIgnore
    @ApiOperation("农气预报-日农事活动预报-保存农事活动预报数据")
    @PostMapping("/saveAgriSrvFcst")
    public SysResult saveAgriSrvFcst(@RequestBody AgriSrvFcst agriSrvFcst) {
        AgriSrvFcst save = agriSrvFcstService.save(agriSrvFcst);
        return SysResult.success(save);
    }


    @ApiOperation("农气预报-日农事活动预报-获取农事活动类型")
    @GetMapping("/getAgriServiceIndexName")
    public SysResult getAgriServiceIndexName() {
        List<String> indexName = agriServiceIndexService.getIndexName();
        if (indexName == null) {
            return SysResult.fail("无");
        }
        return SysResult.success(indexName);
    }


    /************************
     * @Author : HuangDeYin Jolly
     * @Description :
     * @param forecastDate : 日期
     * @param timeChar :时次
     * @param indexName : 类型
     ************************/
    @ApiOperation("农气预报-日农事活动预报-预报查询")
    @GetMapping(value = "forecastQuery")
    public SysResult forecastQuery(String forecastDate, String timeChar, String indexName) {
        List<AgriSrvFcstDto> agriSrvFcstDtoList = agriSrvFcstService.forecastQuery(forecastDate, timeChar, indexName);
        if (!(agriSrvFcstDtoList.size() > 0)) {
            return SysResult.fail("无");
        }
        return SysResult.success(agriSrvFcstDtoList);
    }


    @ApiIgnore
    @ApiOperation("农气预报-日农事活动预报-获取类型indexName")
    @GetMapping(value = "selectIndexName")
    public SysResult selectIndexName(){
        List<String> list = dailyAgriculturalForecastService.selectIndexName();
        return SysResult.success(list);
    }

    @ApiOperation("农气预报-日农事活动预报-产品生成")
    @GetMapping(value = "DailyAgriculturalActivityForecast")
    public Object dailyAgriculturalActivityForecast(String maker, String forecastDate, String timeChar, String indexName){
        return dailyAgriculturalForecastService.dailyAgriculturalActivityForecast(maker, forecastDate, timeChar, indexName);
    }

    @ApiOperation("获取产品模板")
    @GetMapping("/getTemplate")
    public Object getTemplate() {
        //模板路径
        String templatePath;
        ApplicationHome applicationHome = new ApplicationHome(this.getClass());
        if (active.equals("dev")) {
            templatePath = applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath() +
                    "\\src\\main\\resources\\templates\\dailyAgriculturalForecastTemp\\喷药收晒施肥预报.docx";
        } else {
            templatePath = applicationHome.getSource().getParentFile() +
                    "\\resource\\templates\\dailyAgriculturalForecastTemp\\喷药收晒施肥预报.docx";
        }
        // 设置前端响应
        byte[] buffer = new byte[1024];
        int bytesRead;
        try (
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                FileInputStream inputStream = new FileInputStream(templatePath);
        ) {
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, bytesRead);
            }
            byte[] byteArray = byteArrayOutputStream.toByteArray();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentDispositionFormData("attachment", "喷药收晒施肥预报.docx");
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentLength(byteArray.length);
            return new ResponseEntity<>(byteArray, headers, HttpStatus.OK);
        } catch (Exception e) {
            System.err.println("模板下载失败");
        }
        return null;
    }

}
